/***
*
* File: wp_load.nut
* Auth: Damian Wojdat
* Date: 20.10.2012
* Desc: Data loading functions.
*
*************************************************************************************************************/

function loadVehModelsData()
{
	local result;
	local pvar;
	local i = 0;
	if(result = sql.query_assoc("SELECT * FROM " + MYSQL_IVMP_VEHICLES_MODELS))
	{
		if(result.len() == 0)
		{
			log("[INFO:] No vehicles models data !");
			return 1;
		}
		else
		{
			foreach(pvar in result)
			{
				vehModelsData[i].model <- pvar.model.tointeger();
				vehModelsData[i].tankSize <- pvar.tankSize.tointeger();
				vehModelsData[i].fuelType <- pvar.fuelType.tointeger();
				vehModelsData[i].combustion <- pvar.combustion.tointeger();
				vehModelsData[i].gasModable <- pvar.gasModable.tointeger();
				
				i++;
			}
		}
		log("[INFO:] Loaded " + i + " vehicles models data");
	}
	else
		log("[WARNING:] Can't load vehicles models data !");
	return 1;
}

function loadVehicles()
{
	local result;
	local pvar;
	local vehID = 0;
	if(result = sql.query_assoc("SELECT * FROM " + MYSQL_IVMP_VEHICLES))
	{
		if(result.len() == 0)
		{
			log("[INFO:] No vehicles in database !");
			return 1;
		}
		else
		{
			foreach(pvar in result)
			{
				while(pvar.ID_InGame.tointeger() > vehID)
				{
					vehData[vehID] <- vehicleData();
					vehData[vehID].id_inGame = createVehicle(106, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0);					
					setVehicleComponent(vehID, 0, true);					
					vehID++;
				}
				vehData[vehID] <- vehicleData();
				vehData[vehID].id_inGame = createVehicle(pvar.model.tointeger(), pvar.posX.tofloat(), pvar.posY.tofloat(), pvar.posZ.tofloat(), 0.0, 0.0, pvar.posAngle.tofloat(), pvar.color1.tointeger(), pvar.color2.tointeger(), pvar.color3.tointeger(), pvar.color4.tointeger());
				vehData[vehID].model = pvar.model.tointeger();
				vehData[vehID].posX = pvar.posX.tofloat();
				vehData[vehID].posY = pvar.posY.tofloat();
				vehData[vehID].posZ = pvar.posZ.tofloat();
				vehData[vehID].posAngle = pvar.posAngle.tofloat();
				vehData[vehID].color1 = pvar.color1.tointeger();
				vehData[vehID].color2 = pvar.color2.tointeger();
				vehData[vehID].color3 = pvar.color3.tointeger();
				vehData[vehID].color4 = pvar.color4.tointeger();
				vehData[vehID].faction1 = pvar.faction.tointeger();
				vehData[vehID].owned = pvar.owned.tointeger();
				vehData[vehID].owner = pvar.owner.tostring();
				vehData[vehID].price = pvar.price.tointeger();
				vehData[vehID].engineOn = pvar.engineOn.tointeger();
				vehData[vehID].serial = pvar.serial.tostring();
				vehData[vehID].lastDriver = pvar.lastDriver.tostring();
				vehData[vehID].fuel = pvar.fuel.tointeger();
				vehData[vehID].fuelType = pvar.fuelType.tointeger();
				vehData[vehID].dirtLvl = pvar.dirtLvl.tofloat();
				vehData[vehID].locked = pvar.locked.tointeger();
				vehData[vehID].counter = pvar.counter.tofloat();
				vehData[vehID].cb = pvar.cb.tointeger();
				vehData[vehID].cbOn = pvar.cb.tointeger();
				vehData[vehID].cbChannel = pvar.cbChannel.tointeger();
				
				if(pvar.lights.tointeger())
					vehData[vehID].lights = true;
				else
					vehData[vehID].lights = false;
				if(pvar.indicator1.tointeger())
					vehData[vehID].indicator1 = true;
				else
					vehData[vehID].indicator1 = false;
				if(pvar.indicator2.tointeger())
					vehData[vehID].indicator2 = true;
				else
					vehData[vehID].indicator2 = false;
				if(pvar.indicator3.tointeger())
					vehData[vehID].indicator3 = true;
				else
					vehData[vehID].indicator3 = false;
				if(pvar.indicator4.tointeger())
					vehData[vehID].indicator4 = true;
				else
					vehData[vehID].indicator4 = false;
				if(pvar.sirenState.tointeger())
					vehData[vehID].sirenState = true;
				else
					vehData[vehID].sirenState = false;
				
				setVehicleComponent(vehID, 0, true);
				setVehicleDirtLevel(vehID, vehData[vehID].dirtLvl);
				setVehicleSirenState(vehID, vehData[vehID].sirenState);
				setVehicleLights(vehID, vehData[vehID].lights);
				if(vehData[vehID].engineOn)
					setVehicleEngineState(vehID, true);
				else
					setVehicleEngineState(vehID, false);
				setVehicleLocked(vehID, vehData[vehID].locked);
				setVehicleIndicators(vehID, vehData[vehID].indicator1, vehData[vehID].indicator2, vehData[vehID].indicator3, vehData[vehID].indicator4);
				
				vehID++;
				vehiclesCount++;
			}
		}
		log("[INFO:] Loaded " + vehiclesCount + " vehicles");
	}
	else
		log("[WARNING:] Can't load vehicles !");
	return 1;
}

function saveDynamicVehicles()
{
	for(local i = 0; i < MAX_VEHICLES; i++)
	{
		if(isVehicleValid(i))
		{
			//local vModel = getVehicleModel(i.tointeger());
			local vModel = vehicleModel[i];
			local vCoords = getVehicleCoordinates(i);
			local vAngle = getVehicleHeading(i);
			//local vColors = getVehicleColor(i);
			local string;
			local result;
			string = "SELECT * FROM " + MYSQL_IVMP_VEHICLES + " WHERE ID_InGame = " + i + "";
			if(result = sql.query_assoc(string))
			{
				if(result.len() == 0)
				{
					//string = "INSERT INTO " + MYSQL_IVMP_USERS + " (model, posX, posY, posZ, posAngle, color1, color2, color3, color4) VALUES(" + vModel + " , " + vCoords[0] + " , " + vCoords[1] + " , " + vCoords[2] + " , " + vAngle + " , " + vColors[0] + " , " + vColors[1] + " , " + vColors[2] + " , " + vColors[3] + ")";
					string = "INSERT INTO " + MYSQL_IVMP_VEHICLES + " (model, posX, posY, posZ, posAngle, color1, color2, color3, color4) VALUES (" + vModel + " , " + vCoords[0].tofloat() + " , " + vCoords[1].tofloat() + " , " + vCoords[2].tofloat() + " , " + vAngle.tofloat() + " , 0, 0, 0, 0)";
					sql.query(string);
				}
				else
				{
					//string = "UPDATE " + MYSQ_IVMP_VEHICLES + " SET model = '" + vModel + "' , posX = '" + vCoords[0] + "' , posY = '" + vCoords[1] + "' , posZ = '" + vCoords[2] + "' , posAngle = '" + vAngle + "' , color1 = '" + vColors[0] + "' , color2 = '" + vColors[1] + "' , color3 = '" + vColors[2] + "' , color4 = '" + vColors[3] + "' , ID_InGame = '" + i + "'";
					string = "UPDATE " + MYSQ_IVMP_VEHICLES + " SET model = '" + vModel + "' , posX = '" + vCoords[0] + "' , posY = '" + vCoords[1] + "' , posZ = '" + vCoords[2] + "' , posAngle = '" + vAngle + "' , color1 = '0' , color2 = '0' , color3 = '0' , color4 = '0'  , ID_InGame = '" + i + "'";
					sql.query(string);
				}
			}
		}
	}
	return 1;
}
	
function loadBuildings()
{
	local result;
	local pvar;
	result = sql.query_assoc("SELECT * FROM " + MYSQL_IVMP_BUILDINGS + "");
	if(result.len() == 0)
	{
		log("[INFO:] No buildings in database !");
		return 1;
	}
	else
	{
		foreach(pvar in result)
		{
			bData[buildingCount] <- {};
			bData[buildingCount].name <- pvar.name;
			bData[buildingCount].owner <- pvar.owner;
			bData[buildingCount].owned <- pvar.owned.tointeger();
			bData[buildingCount].interior <- pvar.interior.tointeger();
			bData[buildingCount].posXEntrance <- pvar.posXEntrance.tofloat();
			bData[buildingCount].posYEntrance <- pvar.posYEntrance.tofloat();
			bData[buildingCount].posZEntrance <- pvar.posZEntrance.tofloat();
			bData[buildingCount].posAngleEntrance <- pvar.posAngleEntrance.tofloat();
			bData[buildingCount].posXExit <- pvar.posXExit.tofloat();
			bData[buildingCount].posYExit <- pvar.posYExit.tofloat();
			bData[buildingCount].posZExit <- pvar.posZExit.tofloat();
			bData[buildingCount].posAngleExit <- pvar.posAngleExit.tofloat();
			bData[buildingCount].type <- pvar.type.tointeger();
			bData[buildingCount].faction1 <- pvar.faction.tointeger();
			bData[buildingCount].item1 <- pvar.item1.tointeger();
			bData[buildingCount].item1type <- pvar.item1type.tointeger();
			bData[buildingCount].item1option <- pvar.item1option.tointeger();
			bData[buildingCount].item1option2 <- pvar.item1option2.tointeger();
			bData[buildingCount].item1option3 <- pvar.item1option3.tointeger();
			bData[buildingCount].item1option4 <- pvar.item1option4.tointeger();
			bData[buildingCount].item1option5 <- pvar.item1option5;
			bData[buildingCount].item1option6 <- pvar.item1option6.tointeger();
			bData[buildingCount].item2 <- pvar.item2.tointeger();
			bData[buildingCount].item2type <- pvar.item2type.tointeger();
			bData[buildingCount].item2option <- pvar.item2option.tointeger();
			bData[buildingCount].item2option2 <- pvar.item2option2.tointeger();
			bData[buildingCount].item2option3 <- pvar.item2option3.tointeger();
			bData[buildingCount].item2option4 <- pvar.item2option4.tointeger();
			bData[buildingCount].item2option5 <- pvar.item2option5;
			bData[buildingCount].item2option6 <- pvar.item2option6.tointeger();
			bData[buildingCount].item3 <- pvar.item3.tointeger();
			bData[buildingCount].item3type <- pvar.item3type.tointeger();
			bData[buildingCount].item3option <- pvar.item3option.tointeger();
			bData[buildingCount].item3option2 <- pvar.item3option2.tointeger();
			bData[buildingCount].item3option3 <- pvar.item3option3.tointeger();
			bData[buildingCount].item3option4 <- pvar.item3option4.tointeger();
			bData[buildingCount].item3option5 <- pvar.item3option5;
			bData[buildingCount].item3option6 <- pvar.item3option6.tointeger();
			bData[buildingCount].item4 <- pvar.item4.tointeger();
			bData[buildingCount].item4type <- pvar.item4type.tointeger();
			bData[buildingCount].item4option <- pvar.item4option.tointeger();
			bData[buildingCount].item4option2 <- pvar.item4option2.tointeger();
			bData[buildingCount].item4option3 <- pvar.item4option3.tointeger();
			bData[buildingCount].item4option4 <- pvar.item4option4.tointeger();
			bData[buildingCount].item4option5 <- pvar.item4option5;
			bData[buildingCount].item4option6 <- pvar.item4option6.tointeger();
			bData[buildingCount].item5 <- pvar.item5.tointeger();
			bData[buildingCount].item5type <- pvar.item5type.tointeger();
			bData[buildingCount].item5option <- pvar.item5option.tointeger();
			bData[buildingCount].item5option2 <- pvar.item5option2.tointeger();
			bData[buildingCount].item5option3 <- pvar.item5option3.tointeger();
			bData[buildingCount].item5option4 <- pvar.item5option4.tointeger();
			bData[buildingCount].item5option5 <- pvar.item5option5;
			bData[buildingCount].item5option6 <- pvar.item5option6.tointeger();
			bData[buildingCount].item6 <- pvar.item6.tointeger();
			bData[buildingCount].item6type <- pvar.item6type.tointeger();
			bData[buildingCount].item6option <- pvar.item6option.tointeger();
			bData[buildingCount].item6option2 <- pvar.item6option2.tointeger();
			bData[buildingCount].item6option3 <- pvar.item6option3.tointeger();
			bData[buildingCount].item6option4 <- pvar.item6option4.tointeger();
			bData[buildingCount].item6option5 <- pvar.item6option5;
			bData[buildingCount].item6option6 <- pvar.item6option6.tointeger();
			bData[buildingCount].item7 <- pvar.item7.tointeger();
			bData[buildingCount].item7type <- pvar.item7type.tointeger();
			bData[buildingCount].item7option <- pvar.item7option.tointeger();
			bData[buildingCount].item7option2 <- pvar.item7option2.tointeger();
			bData[buildingCount].item7option3 <- pvar.item7option3.tointeger();
			bData[buildingCount].item7option4 <- pvar.item7option4.tointeger();
			bData[buildingCount].item7option5 <- pvar.item7option5;
			bData[buildingCount].item7option6 <- pvar.item7option6.tointeger();
			bData[buildingCount].item8 <- pvar.item8.tointeger();
			bData[buildingCount].item8type <- pvar.item8type.tointeger();
			bData[buildingCount].item8option <- pvar.item8option.tointeger();
			bData[buildingCount].item8option2 <- pvar.item8option2.tointeger();
			bData[buildingCount].item8option3 <- pvar.item8option3.tointeger();
			bData[buildingCount].item8option4 <- pvar.item8option4.tointeger();
			bData[buildingCount].item8option5 <- pvar.item8option5;
			bData[buildingCount].item8option6 <- pvar.item8option6.tointeger();
			bData[buildingCount].item9 <- pvar.item9.tointeger();
			bData[buildingCount].item9type <- pvar.item9type.tointeger();
			bData[buildingCount].item9option <- pvar.item9option.tointeger();
			bData[buildingCount].item9option2 <- pvar.item9option2.tointeger();
			bData[buildingCount].item9option3 <- pvar.item9option3.tointeger();
			bData[buildingCount].item9option4 <- pvar.item9option4.tointeger();
			bData[buildingCount].item9option5 <- pvar.item9option5;
			bData[buildingCount].item9option6 <- pvar.item9option6.tointeger();
			bData[buildingCount].item10 <- pvar.item10.tointeger();
			bData[buildingCount].item10type <- pvar.item10type.tointeger();
			bData[buildingCount].item10option <- pvar.item10option.tointeger();
			bData[buildingCount].item10option2 <- pvar.item10option2.tointeger();
			bData[buildingCount].item10option3 <- pvar.item10option3.tointeger();
			bData[buildingCount].item10option4 <- pvar.item10option4.tointeger();
			bData[buildingCount].item10option5 <- pvar.item10option5;
			bData[buildingCount].item10option6 <- pvar.item10option6.tointeger();
			bData[buildingCount].item11 <- pvar.item11.tointeger();
			bData[buildingCount].item11type <- pvar.item11type.tointeger();
			bData[buildingCount].item11option <- pvar.item11option.tointeger();
			bData[buildingCount].item11option2 <- pvar.item11option2.tointeger();
			bData[buildingCount].item11option3 <- pvar.item11option3.tointeger();
			bData[buildingCount].item11option4 <- pvar.item11option4.tointeger();
			bData[buildingCount].item11option5 <- pvar.item11option5;
			bData[buildingCount].item11option6 <- pvar.item11option6.tointeger();
			bData[buildingCount].item12 <- pvar.item12.tointeger();
			bData[buildingCount].item12type <- pvar.item12type.tointeger();
			bData[buildingCount].item12option <- pvar.item12option.tointeger();
			bData[buildingCount].item12option2 <- pvar.item12option2.tointeger();
			bData[buildingCount].item12option3 <- pvar.item12option3.tointeger();
			bData[buildingCount].item12option4 <- pvar.item12option4.tointeger();
			bData[buildingCount].item12option5 <- pvar.item12option5;
			bData[buildingCount].item12option6 <- pvar.item12option6.tointeger();
			bData[buildingCount].item13 <- pvar.item13.tointeger();
			bData[buildingCount].item13type <- pvar.item13type.tointeger();
			bData[buildingCount].item13option <- pvar.item13option.tointeger();
			bData[buildingCount].item13option2 <- pvar.item13option2.tointeger();
			bData[buildingCount].item13option3 <- pvar.item13option3.tointeger();
			bData[buildingCount].item13option4 <- pvar.item13option4.tointeger();
			bData[buildingCount].item13option5 <- pvar.item13option5;
			bData[buildingCount].item13option6 <- pvar.item13option6.tointeger();
			bData[buildingCount].item14 <- pvar.item14.tointeger();
			bData[buildingCount].item14type <- pvar.item14type.tointeger();
			bData[buildingCount].item14option <- pvar.item14option.tointeger();
			bData[buildingCount].item14option2 <- pvar.item14option2.tointeger();
			bData[buildingCount].item14option3 <- pvar.item14option3.tointeger();
			bData[buildingCount].item14option4 <- pvar.item14option4.tointeger();
			bData[buildingCount].item14option5 <- pvar.item14option5;
			bData[buildingCount].item14option6 <- pvar.item14option6.tointeger();
			bData[buildingCount].item15 <- pvar.item15.tointeger();
			bData[buildingCount].item15type <- pvar.item15type.tointeger();
			bData[buildingCount].item15option <- pvar.item15option.tointeger();
			bData[buildingCount].item15option2 <- pvar.item15option2.tointeger();
			bData[buildingCount].item15option3 <- pvar.item15option3.tointeger();
			bData[buildingCount].item15option4 <- pvar.item15option4.tointeger();
			bData[buildingCount].item15option5 <- pvar.item15option5;
			bData[buildingCount].item15option6 <- pvar.item15option6.tointeger();
			bData[buildingCount].item16 <- pvar.item16.tointeger();
			bData[buildingCount].item16type <- pvar.item16type.tointeger();
			bData[buildingCount].item16option <- pvar.item16option.tointeger();
			bData[buildingCount].item16option2 <- pvar.item16option2.tointeger();
			bData[buildingCount].item16option3 <- pvar.item16option3.tointeger();
			bData[buildingCount].item16option4 <- pvar.item16option4.tointeger();
			bData[buildingCount].item16option5 <- pvar.item16option5;
			bData[buildingCount].item16option6 <- pvar.item16option6.tointeger();
			bData[buildingCount].item17 <- pvar.item17.tointeger();
			bData[buildingCount].item17type <- pvar.item17type.tointeger();
			bData[buildingCount].item17option <- pvar.item17option.tointeger();
			bData[buildingCount].item17option2 <- pvar.item17option2.tointeger();
			bData[buildingCount].item17option3 <- pvar.item17option3.tointeger();
			bData[buildingCount].item17option4 <- pvar.item17option4.tointeger();
			bData[buildingCount].item17option5 <- pvar.item17option5;
			bData[buildingCount].item17option6 <- pvar.item17option6.tointeger();
			bData[buildingCount].item18 <- pvar.item18.tointeger();
			bData[buildingCount].item18type <- pvar.item18type.tointeger();
			bData[buildingCount].item18option <- pvar.item18option.tointeger();
			bData[buildingCount].item18option2 <- pvar.item18option2.tointeger();
			bData[buildingCount].item18option3 <- pvar.item18option3.tointeger();
			bData[buildingCount].item18option4 <- pvar.item18option4.tointeger();
			bData[buildingCount].item18option5 <- pvar.item18option5;
			bData[buildingCount].item18option6 <- pvar.item18option6.tointeger();
			bData[buildingCount].item19 <- pvar.item19.tointeger();
			bData[buildingCount].item19type <- pvar.item19type.tointeger();
			bData[buildingCount].item19option <- pvar.item19option.tointeger();
			bData[buildingCount].item19option2 <- pvar.item19option2.tointeger();
			bData[buildingCount].item19option3 <- pvar.item19option3.tointeger();
			bData[buildingCount].item19option4 <- pvar.item19option4.tointeger();
			bData[buildingCount].item19option5 <- pvar.item19option5;
			bData[buildingCount].item19option6 <- pvar.item19option6.tointeger();
			bData[buildingCount].item20 <- pvar.item20.tointeger();
			bData[buildingCount].item20type <- pvar.item20type.tointeger();
			bData[buildingCount].item20option <- pvar.item20option.tointeger();
			bData[buildingCount].item20option2 <- pvar.item20option2.tointeger();
			bData[buildingCount].item20option3 <- pvar.item20option3.tointeger();
			bData[buildingCount].item20option4 <- pvar.item20option4.tointeger();
			bData[buildingCount].item20option5 <- pvar.item20option5;
			bData[buildingCount].item20option6 <- pvar.item20option6.tointeger();
			
			// bData[buildingCount].entranceCPID <- createCheckpoint( CP_TYPE_ARROW_TUBE, bData[buildingCount].posXEntrance, bData[buildingCount].posYEntrance, bData[buildingCount].posZEntrance, bData[buildingCount].posXEntrance, bData[buildingCount].posYEntrance, bData[buildingCount].posZEntrance-100.0, 0.2);
			// bData[buildingCount].exitCPID <- createCheckpoint( CP_TYPE_ARROW_TUBE, bData[buildingCount].posXExit, bData[buildingCount].posYExit, bData[buildingCount].posZExit, bData[buildingCount].posXExit, bData[buildingCount].posYExit, bData[buildingCount].posZExit-100.0, 0.2);
		
			if(bData[buildingCount].type == 0) // gov
			{
				bData[buildingCount].entranceBlipID <- createBlip( 92, bData[buildingCount].posXEntrance, bData[buildingCount].posYEntrance, bData[buildingCount].posZEntrance);
				setBlipColor( bData[buildingCount].entranceBlipID, COLOR_WHITE);
				toggleBlipShortRange( bData[buildingCount].entranceBlipID, true);
			}
			else if(bData[buildingCount].type == 1) // police
			{
				bData[buildingCount].entranceBlipID <- createBlip( 60, bData[buildingCount].posXEntrance, bData[buildingCount].posYEntrance, bData[buildingCount].posZEntrance);
				setBlipColor( bData[buildingCount].entranceBlipID, COLOR_WHITE);
				toggleBlipShortRange( bData[buildingCount].entranceBlipID, true);
			}
			else if(bData[buildingCount].type == 2) // hospital
			{
				bData[buildingCount].entranceBlipID <- createBlip( 62, bData[buildingCount].posXEntrance, bData[buildingCount].posYEntrance, bData[buildingCount].posZEntrance);
				setBlipColor( bData[buildingCount].entranceBlipID, COLOR_WHITE);
				toggleBlipShortRange( bData[buildingCount].entranceBlipID, true);
			}
			else if(bData[buildingCount].type == 3) // house
			{
				bData[buildingCount].entranceBlipID <- createBlip( 29, bData[buildingCount].posXEntrance, bData[buildingCount].posYEntrance, bData[buildingCount].posZEntrance);
				setBlipColor( bData[buildingCount].entranceBlipID, COLOR_WHITE);
				toggleBlipShortRange( bData[buildingCount].entranceBlipID, true);
			}
			else if(bData[buildingCount].type == 4) // biz clothes
			{
				bData[buildingCount].entranceBlipID <- createBlip( 50, bData[buildingCount].posXEntrance, bData[buildingCount].posYEntrance, bData[buildingCount].posZEntrance);
				setBlipColor( bData[buildingCount].entranceBlipID, COLOR_WHITE);
				toggleBlipShortRange( bData[buildingCount].entranceBlipID, true);
			}
			else if(bData[buildingCount].type == 5) // biz bar
			{
				bData[buildingCount].entranceBlipID <- createBlip( 47, bData[buildingCount].posXEntrance, bData[buildingCount].posYEntrance, bData[buildingCount].posZEntrance);
				setBlipColor( bData[buildingCount].entranceBlipID, COLOR_WHITE);
				toggleBlipShortRange( bData[buildingCount].entranceBlipID, true);
			}
			else if(bData[buildingCount].type == 6) // biz ammu
			{
				bData[buildingCount].entranceBlipID <- createBlip( 59, bData[buildingCount].posXEntrance, bData[buildingCount].posYEntrance, bData[buildingCount].posZEntrance);
				setBlipColor( bData[buildingCount].entranceBlipID, COLOR_WHITE);
				toggleBlipShortRange( bData[buildingCount].entranceBlipID, true);
			}
			else if(bData[buildingCount].type == 7) // biz restaurant
			{
				bData[buildingCount].entranceBlipID <- createBlip( 57, bData[buildingCount].posXEntrance, bData[buildingCount].posYEntrance, bData[buildingCount].posZEntrance);
				setBlipColor( bData[buildingCount].entranceBlipID, COLOR_WHITE);
				toggleBlipShortRange( bData[buildingCount].entranceBlipID, true);
			}
			else if(bData[buildingCount].type == 8) // biz clucking
			{
				bData[buildingCount].entranceBlipID <- createBlip( 22, bData[buildingCount].posXEntrance, bData[buildingCount].posYEntrance, bData[buildingCount].posZEntrance);
				setBlipColor( bData[buildingCount].entranceBlipID, COLOR_WHITE);
				toggleBlipShortRange( bData[buildingCount].entranceBlipID, true);
			}
			else if(bData[buildingCount].type == 9) // biz burger
			{
				bData[buildingCount].entranceBlipID <- createBlip( 21, bData[buildingCount].posXEntrance, bData[buildingCount].posYEntrance, bData[buildingCount].posZEntrance);
				setBlipColor( bData[buildingCount].entranceBlipID, COLOR_WHITE);
				toggleBlipShortRange( bData[buildingCount].entranceBlipID, true);
			}
			else if(bData[buildingCount].type == 10) // biz other
			{
				bData[buildingCount].entranceBlipID <- createBlip( 32, bData[buildingCount].posXEntrance, bData[buildingCount].posYEntrance, bData[buildingCount].posZEntrance);
				setBlipColor( bData[buildingCount].entranceBlipID, COLOR_LIGHTBLUE);
				toggleBlipShortRange( bData[buildingCount].entranceBlipID, true);
			}
			else
			{
				bData[buildingCount].entranceBlipID <- createBlip( 32, bData[buildingCount].posXEntrance, bData[buildingCount].posYEntrance, bData[buildingCount].posZEntrance);
				setBlipColor( bData[buildingCount].entranceBlipID, COLOR_WHITE);
				toggleBlipShortRange( bData[buildingCount].entranceBlipID, true);
			}
			
			buildingCount++;
		}
	}
	log("[INFO:] Loaded " + buildingCount + " buildings");
	return 1;
}

function saveDynamicBuildings()
{
	for(local i = 0; i < buildingCount; i++)
	{
		local string;
		local result;
		string = "SELECT * FROM " + MYSQL_IVMP_BUILDINGS + " WHERE ID = " + i;
		if(result = sql.query_assoc(string))
		{
			if(result.len() == 0)
			{
				string = "INSERT INTO " + MYSQL_IVMP_BUILDINGS + " (name , interior , posXEntrance , posYEntrance , posZEntrance , posAngleEntrance , posXExit , posYExit , posZExit , posAngleExit , buildingType) VALUES ('" + bData[i].name + "' , '" + bData[i].interior + "' , '" + bData[i].posXEntrance + "' , '" + bData[i].posYEntrance + "' , '" + bData[i].posAngleEntrance + "' , '" + bData[i].posXExit + "' , '" + bData[i].posYExit + "' , '" + bData[i].posZExit + "' , '" + bData[i].posAngleExit + "' , '" + bData[i].buildingType + "')";
				sql.query(string);
			}
			else
			{
				string = "UPDATE " + MYSQ_IVMP_BUILDINGS + " SET name = '" + bData[i].name + "' , interior = '" + bData[i].interior + "' , posXEntrance = '" + bData[i].posXEntrance + "' , posYEntrance = '" + bData[i].posYEntrance + "' , posZEntrance = '" + bData[i].posZEntrance + "' , posAngleEntrance = '" + bData[i].posAngleEntrance + "' , posXExit = '" + bData[i].posXExit + "' , posYExit = '" + bData[i].posYExit + "' , posZExit = '" + bData[i].posZExit + "' , posAngleExit = '" + bData[i].posAngleExit + "' , buildingType = '" + bData[i].buildingType + ")";
				sql.query(string);
			}
		}
	}
	return 1;
}

function loadFactions()
{
	local result;
	local pvar;
	
	if(result = sql.query_assoc("SELECT * FROM " + MYSQL_IVMP_FACTIONS + ""))
	{
		if(result.len() == 0)
		{
			log("[INFO:] No factions in database !");
			return 1;
		}
		else
		{
			foreach(pvar in result)
			{
				fData[factionsCount] <- {};
				fData[factionsCount].name <- pvar.name;
				fData[factionsCount].rankCount <- pvar.rankCount;
				fData[factionsCount].rank1 <- pvar.rank1;
				fData[factionsCount].rank2 <- pvar.rank2;
				fData[factionsCount].rank3 <- pvar.rank3;
				fData[factionsCount].rank4 <- pvar.rank4;
				fData[factionsCount].rank5 <- pvar.rank5;
				fData[factionsCount].rank6 <- pvar.rank6;
				fData[factionsCount].rank7 <- pvar.rank7;
				fData[factionsCount].rank8 <- pvar.rank8;
				fData[factionsCount].rank9 <- pvar.rank9;
				fData[factionsCount].rank10 <- pvar.rank10;
				fData[factionsCount].rank11 <- pvar.rank11;
				fData[factionsCount].rank12 <- pvar.rank12;
				fData[factionsCount].rank13 <- pvar.rank13;
				fData[factionsCount].rank14 <- pvar.rank14;
				fData[factionsCount].rank15 <- pvar.rank15;
				fData[factionsCount].rank1reward <- pvar.rank1reward;
				fData[factionsCount].rank2reward <- pvar.rank2reward;
				fData[factionsCount].rank3reward <- pvar.rank3reward;
				fData[factionsCount].rank4reward <- pvar.rank4reward;
				fData[factionsCount].rank5reward <- pvar.rank5reward;
				fData[factionsCount].rank6reward <- pvar.rank6reward;
				fData[factionsCount].rank7reward <- pvar.rank7reward;
				fData[factionsCount].rank8reward <- pvar.rank8reward;
				fData[factionsCount].rank9reward <- pvar.rank9reward;
				fData[factionsCount].rank10reward <- pvar.rank10reward;
				fData[factionsCount].rank11reward <- pvar.rank11reward;
				fData[factionsCount].rank12reward <- pvar.rank12reward;
				fData[factionsCount].rank13reward <- pvar.rank13reward;
				fData[factionsCount].rank14reward <- pvar.rank14reward;
				fData[factionsCount].rank15reward <- pvar.rank15reward;
				fData[factionsCount].skin1 <- pvar.skin1;
				fData[factionsCount].skin2 <- pvar.skin2;
				fData[factionsCount].skin3 <- pvar.skin3;
				fData[factionsCount].skin4 <- pvar.skin4;
				fData[factionsCount].skin5 <- pvar.skin5;
				fData[factionsCount].skin6 <- pvar.skin6;
				fData[factionsCount].skin7 <- pvar.skin7;
				fData[factionsCount].skin8 <- pvar.skin8;
				fData[factionsCount].skin9 <- pvar.skin9;
				fData[factionsCount].skin10 <- pvar.skin10;
				fData[factionsCount].skin11 <- pvar.skin11;
				fData[factionsCount].skin12 <- pvar.skin12;
				fData[factionsCount].skin13 <- pvar.skin13;
				fData[factionsCount].skin14 <- pvar.skin14;
				fData[factionsCount].skin15 <- pvar.skin15;
				fData[factionsCount].useSkin <- pvar.useSkin;
				fData[factionsCount].joinRank <- pvar.joinRank;
				fData[factionsCount].type <- pvar.type;
				fData[factionsCount].maxVehs <- pvar.maxVehs;
				fData[factionsCount].dutyX <- pvar.dutyX;
				fData[factionsCount].dutyY <- pvar.dutyY;
				fData[factionsCount].dutyZ <- pvar.dutyZ;
				fData[factionsCount].duty2X <- pvar.duty2X;
				fData[factionsCount].duty2Y <- pvar.duty2Y;
				fData[factionsCount].duty2Z <- pvar.duty2Z;
				fData[factionsCount].duty3X <- pvar.duty3X;
				fData[factionsCount].duty3Y <- pvar.duty3Y;
				fData[factionsCount].duty3Z <- pvar.duty3Z;
				fData[factionsCount].duty4X <- pvar.duty4X;
				fData[factionsCount].duty4Y <- pvar.duty4Y;
				fData[factionsCount].duty4Z <- pvar.duty4Z;
				fData[factionsCount].kills <- 0;
				
				factionsCount++;
			}
		}
		log("[INFO:] Loaded " + factionsCount + " factions");
	}
	else
	{
		log("[WARNING:] Can't load factions !");
		return 1;
	}
	return 1;
}

function loadDefaultSpawnData()
{
	local fData = EasyINI("defaultspawndata.ini");
	if(!fData.keyExists("pos", "posX"))
	{
		log("[WARNING:] Missing default spawn position value (X). Check defaultspawndata.ini file.");
		defaultSpawnData.posX = 0.0;
	}
	else
	{
		defaultSpawnData.posX = fData.getKey("pos", "posX");
	}
	if(!fData.keyExists("pos", "posY"))
	{
		log("[WARNING:] Missing default spawn position value (Y). Check defaultspawndata.ini file.");
		defaultSpawnData.posY = 0.0;
	}
	else
	{
		defaultSpawnData.posY = fData.getKey("pos", "posY");
	}
	if(!fData.keyExists("pos", "posZ"))
	{
		log("[WARNING:] Missing default spawn position value (Z). Check defaultspawndata.ini file.");
		defaultSpawnData.posZ = 0.0;
	}
	else
	{
		defaultSpawnData.posZ = fData.getKey("pos", "posZ");
	}
	if(!fData.keyExists("pos", "posA"))
	{
		log("[WARNING:] Missing default spawn position value (A). Check defaultspawndata.ini file.");
		defaultSpawnData.posA = 0.0;
	}
	else
	{
		defaultSpawnData.posA = fData.getKey("pos", "posA");
	}
	if(!fData.keyExists("other", "cash"))
	{
		log("[WARNING:] Missing default cash value. Check defaultspawndata.ini file.");
		defaultSpawnData.cash = 0;
	}
	else
	{
		defaultSpawnData.cash = fData.getKey("other", "cash");
	}
	
	log("[INFO:] Loaded players default spawn data");
}

function saveDefaultSpawnData()
{
	local fData = EasyINI("defaultspawndata.ini");
	fData.setKey("pos", "posX", defaultSpawnData.posX.tostring());
	fData.setKey("pos", "posY", defaultSpawnData.posY.tostring());
	fData.setKey("pos", "posZ", defaultSpawnData.posZ.tostring());
	fData.setKey("pos", "posA", defaultSpawnData.posA.tostring());
	fData.setKey("other", "cash", defaultSpawnData.cash.tostring());
	fData.saveData();
}
